package com.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.base.CommonMethods;
import com.dao.impl.LoginDaoImpl;
import com.db.DBManager;
import com.domain.User;
import com.service.LoginService;

/**
 * 登录服务层实现类
 * 
 * @author 无边的绿波
 * 
 */
@Repository("loginService")
public class LoginServiceImpl implements LoginService {

	@Autowired
	private LoginDaoImpl loginDao;

	@Override
	public int login(User login) {

		String username = login.getUsername();
		String password = login.getPassword();
		String role = login.getRole();

		String sql = "SELECT u.id FROM userinfo AS u WHERE u.userid = '"
				+ username + "' AND u.role = '" + role + "'";
		if (CommonMethods.isEmpty(loginDao.queryData(sql))) {
			return -1;
		}
		sql = "select * from userinfo where userid = '" + username
				+ "'and password = '" + password + "'and role= '" + role + "'";
		boolean flag = false;
		try {
			flag = loginDao.verData(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag ? 1 : 0;
	}

	@Override
	public User getUserInfologin(User login) {
		String username = login.getUsername();
		String sql = "select * from userinfo where userid = '" + username + "'";
		User result = (User) loginDao.queData(sql);
		return result;
	}

	@Override
	public boolean modPwd(User user, String pwd1, String pwd2)
			throws SQLException {
		String userId = user.getUsername();
		String sql = "UPDATE userinfo SET PASSWORD = '" + pwd2
				+ "' WHERE userId = '" + userId + "' AND PASSWORD = '" + pwd1
				+ "'";
		return loginDao.modData(sql);
	}

	@Override
	public boolean resetPwd(String username, String pwd) throws SQLException {
		String sql = "UPDATE userinfo SET PASSWORD = '" + pwd
				+ "' WHERE userId = '" + username + "'";
		return loginDao.modData(sql);
	}

}
